define(['jquery','bootstrap', 'backend', 'table','form','editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'serial/special/index',
                    generate_url: 'serial/special/generate',
                    edit_url: 'serial/special/edit',
                    excel_url: 'serial/special/send',
                    generate_tax_url: 'serial/special/cancel',
                    multi_url: 'serial/special/multi',
                    table: 'serial',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'code', title: __('Code')},
                        {field: 'Userid', title: __('Uid'),visible:false},
                        {field: 'is_send', title: __('Is_send'),formatter: Controller.api.formatter.sends,operate: false},
                        {field: 'is_cancel', title: __('Is_cancel'),formatter: Controller.api.formatter.cancels,operate: false},
                        {field: 'is_use', title: __('Is_use'),events: Controller.api.events.UseStatus,formatter: Controller.api.formatter.uses,operate: false},
                        {
                            field: 'type_pdf',
                            title: __('Type_pdf'),
                              editable: {
                                    type: 'select',
                                    pk: 1,
                                    source: [
                                        {value: '0', text: '无'},
                                        {value: '1', text: __('Type_pdf 1')},
                                        {value: '2', text: __('Type_pdf 2')},
                                        {value: '3', text: __('Type_pdf 3')},
                                    ]
                                }
                            },
                        {field: 'usetime', title: __('Usetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        generate: function () {
          $('#action').change(function () {
            if($('#action').val() == 2)
            {
              $('#userid').css('display','block');
              return false;
            }
            $('#userid').css('display','none');
            return false;
          });
            Controller.api.bindevent();
        },
        send: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
    
        api: {
            bindevent: function () {
              Form.api.bindevent($("form[role=send]"));
              Form.api.bindevent($("form[role=form]"),function (data,ret) {
                if(ret.url)
                {
                  window.open(ret.url);
                }
              });
            },
          formatter: {//渲染的方法
              sends: function (value, row, index) {
                  var color = 'success';
                  var text = '';
                  if(value == 2){
                    color = 'success';
                  }else{
                    color = 'yellow';
                  }
                  return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+row.send_text+'</span>';
                },
                cancels: function (value, row, index) {
                  var color = 'success';
                  var text = '';
                  if(value == 1){
                    color = 'success';
                  }else{
                    color = 'red';
                  }
                  return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+row.cancel_text+'</span>';
                },
             uses: function (value, row, index) {
                 var color = 'success';
                  if(value == 1){
                    color = 'yellow';
                  }
                  if(value == 2){
                       return '<a class="btn btn-xs btn-dialog btn-use"><span class="text-'+color+'"><i class="fa fa-circle"></i> '+row.use_text+'</span> </a>';
                   }else{
                       return '<span class="text-'+color+'"><i class="fa fa-circle"></i>'+row.use_text+'</span>';
                   }  
                },
          },
           events: {//绑定事件的方法
                 UseStatus: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-use': function (e, value, row, index) {
                      console.log(row);
                        e.stopPropagation();
                        var str = '';
                        if(row.userid){
                           str += "微信昵称: <code>" +row.username_text + "</code><br>";
                           str += "手机号码: <code>" +row.phone_text + "</code><br>";
                           str += "真实姓名: <code>" +row.name_text + "</code><br>";
                           str += "订单编号: <code>" +row.nb_text + "</code>";
                        }
                        Layer.alert(str);
                    }
                }
            }
        },

    };
    return Controller;
});